package com.yx.websocketim

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.os.Message

/**
 * Creator:Yx
 * Time:2022/7/12 14:45
 * Description:广播：接受服务端发送的消息
 */
class MsgReceiver : BroadcastReceiver() {

    /**
     * 单例 - 懒汉式
     */
    companion object {
        val instance by lazy(LazyThreadSafetyMode.NONE) {
            MsgReceiver()
        }
    }


    /**
     * BroadcastReceiver.onReceive总是在UI线程中运行 √
     */
    override fun onReceive(context: Context?, intent: Intent?) {
        val msg = intent?.getStringExtra("message") ?: ""
        LogUtils.iYx("——>广播收到<——")
        val mMsg: Message = Message.obtain()
        mMsg.what = 67367
        mMsg.obj = msg
        MainActivity.handler.sendMessage(mMsg)
    }


}